<?php

class WeixinAdmin extends AdminBase
{
	private $nohf = ['groupDoAdd','groupedit', 'groupdel', 'updatecategory', 'doupdatecategory'];
	private $mysql = null;
	private $api = null;

	public function __construct()
	{
		parent::__construct();
		if(!in_array(N_ACTION,$this->nohf)){
			n_mod('skin')->header('admin');
		}
		$this->mysql = n_api('mysql')->cname('weixin');
		$this->api = n_api('weixin');
	}

	public function groupManage()
	{
		$query = "select a.id as sid, a.name as second_category, a.norder as sorder, a.description as sdescription, b.id as fid, b.name as first_category, b.norder as forder, b.description as fdescription from weixin_second_category as a left join weixin_first_category as b on a.fid = b.id where a.isdel = 'N' and b.isdel = 'N' order by a.norder asc";
		$rs = $this->mysql->select($query);
		foreach ($rs as $value) {
			$first_category[$value['fid']] = ['first_category'=>$value['first_category'], 'forder'=>$value['forder'], 'fdescription'=>$value['fdescription']];
			$second_category[$value['fid']][] = ['sid'=>$value['sid'], 'second_category'=>$value['second_category'], 'sorder'=>$value['sorder'], 'sdescription'=>$value['sdescription']];
		}
		$data = ['first_category'=>$first_category, 'second_category'=>$second_category];
		$this->_html('groupManage', $data);
	}

	public function groupAdd()
	{
		$this->_html('groupAdd');
	}

	public function groupDoAdd()
	{
		$param = $this->param("
			name:char:empty,
			norder:char:empty,
			description:char:empty
		");

		$name = $param['name'];
		$query = "select * from weixin_mp_category where name='$name' and isdel ='N'";
		$rs = $this->mysql->fetchFirst($query);
		if (count($rs) > 0)
			exit('组已存在');
		
		$time = time();
		$data = $param;
		$data['ctime'] = $time;
		$data['utime'] = $time;

		$this->mysql->insert('weixin_mp_category', $data);
		echo 'add successfully';
	}

	public function groupEdit()
	{
		$param = $this->param("cid:char:empty,catetype:char:empty");
		$id = $param['cid'];
		$type = $param['catetype'];

		if ($type == 1) {
			$query = "select id, name, norder, description from weixin_first_category where id = $id and isdel = 'N'";
			$rs = $this->mysql->fetchFirst($query);
		} else if ($type == 2) {
			$query = "select id, name, norder, description from weixin_second_category where id = $id and isdel = 'N'";
			$rs = $this->mysql->fetchFirst($query);
		}
		$rs['catetype'] = $type;
		$this->_html('groupEdit', $rs);
	}

	public function groupUpdate()
	{
		$param = $this->param("
			name:char:empty,
			norder:char:empty,
			description:char:empty,
			cid:char:empty,
			catetype:char:empty
		");

		$id = $param['cid'];
		$where = "id = $id";
		unset($param['cid']);
		$type = $param['catetype'];
		unset($param['catetype']);
		$data = $param;
		$data['utime'] = time();

		if ($type == 1)
			$this->mysql->update('weixin_first_category', $data, $where);
		else if ($type == 2)
			$this->mysql->update('weixin_second_category', $data, $where);
		n_mod("skin")->save("保存成功",3);
	}

	public function groupDel()
	{
		$param = $this->param("cid:char:empty,catetype:char:empty");
		$id = $param['cid'];
		$type = $param['catetype'];

		if ($type == 1) {
			$secondCategories = $this->api->getSecondCategorybyFirstId($id);
			$sids = array_keys($secondCategories);
			if ($this->api->checkCategoryNeedByIds($sids)) {
				echo "对应二级目录还有公众号";
				return;
			} else {
				if ($this->mysql->del('weixin_first_category', $id))
					echo 'del successfully';
				else
					echo 'del failure';
			}
		} else if ($type == 2) {
			if ($this->api->checkCategoryNeedByIds($id)) {
				echo "此二级目录还有公共号";
				return;
			} else {
				if ($this->mysql->del('weixin_second_category', $id))
					echo 'del successfully';
				else
					echo 'del failure';
			}
		}
	}

	public function updateCategory()
	{
		$param = $this->param("wxid:char:empty");
		$wxid = $param['wxid'];

		$weixin = n_api('weixin');
		$data['wxInfos'] = $weixin->getWxDetail($wxid);
		$data['categories'] = $weixin->getCategories();
		$this->_html('updateCategory', $data);
	}

	public function doUpdateCategory()
	{
		$param = $this->param("wxid:char:empty,cid:char:empty");
		$wxid = $param['wxid'];
		$cid = $param['cid'];

		$query = "update weixin_main set cid = $cid where wxid = '$wxid'";
		$this->mysql->exec($query);
		echo "ok";
	}

	public function search(){
		
		$param = $this->param("k:char:ignore");
		$rs['param'] = $param;
		if($param['k']!=''){
			$query = "select * from weixin_main where wxname like '%{$param['k']}%'";
			$rs['data'] = $this->mysql->select($query);
			
			$rs['cate'] = $this->api->getCategories();
		}

		$this->_html("adminSearch",$rs);
	}

	public function __destruct(){
		//加入标准尾。
		if(!in_array(N_ACTION,$this->nohf) && 'page'==N_TYPE){
			n_mod('skin')->footer();
		}
	}
}